<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }
/**
 * @author aique
 * 
 */

class SiteMap{
    
    private $statisticURL = '';
    private $db = null;
    public $catArray = array();
    public $bookArray = array();
   
    function __construct()
    {
        $this->db = new DB();
        $this->db->startConnection();
        $this->statisticURL = $this->db->get('configManager')->statisticURL;
        $this->catArray = $this->db->get('catManager')->setPointCat();        
        $this->bookArray = $this->db->get('bookManager')->setPointBook();          
        
        
    }
    
    function __destruct()
    {
        $this->db->closeConnection();
    }

    function setSiteMap()
    {
        
        $file =  'sitemap/sitemap.xml';
        $handle = fopen($file, 'w');
    
        if(!$handle){
         echo "Fail to open : ".$file;
        
         return false;
        }
        else{
            echo "Success!";
            $head = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
            $strCat =  $this->catBlock($this->catArray);
            $strBook = $this->bookBlock($this->bookArray);
            $strStatic = $this->staticBlock();
            $tail = '
            </urlset>';
            $ct = $head.$strBook.$strCat.$strStatic.$tail;
           // $ct = $strBook;
            $content = <<<EOT
$ct

EOT;
        
            fwrite($handle, $content);
        
            fclose($handle);
            
        }
    }
    
    function catBlock($catArray)
    {
        $str = "";
        foreach($catArray as $value){
            $url = $this->statisticURL.'category/'.$value['cid'];
            $str .= $this->oneBlock($url, 'monthly', $value['point']);
        }
        return $str;
        
    }
    
    function bookBlock($bookArray)
    {
        $str = '';
        foreach($bookArray as $value){
            $title = str_replace(" ", "-", $value['title']);
            $url = $this->statisticURL.'detail/'.$value['nid'].'-'.$title;
            if ($value['point']==null){
                $value['point'] = 0.5;
            }            
            $str .= $this->oneBlock($url, 'monthly', $value['point']);
        }
        return $str;
    }
    
    function staticBlock()
    {
        $str = " 
                <url>
                    <loc>$this->statisticURL</loc>
                    <changefreq>daily</changefreq>
                    <priority>0.9</priority>
                </url>
                
                <url>
                    <loc>".$this->statisticURL."about</loc>
                    <changefreq>yearly</changefreq>
                    <priority>0.5</priority>
                </url>
                
                <url>
                    <loc>".$this->statisticURL."admincp/</loc>
                    <changefreq>monthly</changefreq>
                    <priority>0.7</priority>
                </url>
                
                <url>
                    <loc>".$this->statisticURL."contactus</loc>
                    <changefreq>yearly</changefreq>
                    <priority>0.5</priority>
                </url>
                
                <url>
                    <loc>".$this->statisticURL."index</loc>
                    <changefreq>daily</changefreq>
                    <priority>1.0</priority>
                </url>  
  
                <url>
                    <loc>".$this->statisticURL."privacy</loc>
                    <changefreq>monthly</changefreq>
                    <priority>0.5</priority>
                </url>  
  
                <url>
                    <loc>".$this->statisticURL."register</loc>
                    <changefreq>yearly</changefreq>
                    <priority>0.9</priority>
                </url>  
  
                <url>
                    <loc>".$this->statisticURL."services</loc>
                    <changefreq>yearly</changefreq>
                    <priority>0.9</priority>
                </url>  
  
                <url>
                    <loc>".$this->statisticURL."special</loc>
                    <changefreq>daily</changefreq>
                    <priority>0.9</priority>
                </url>
                
                <url>
                    <loc>".$this->statisticURL."book</loc>
                    <changefreq>monthly</changefreq>
                    <priority>0.9</priority>
                </url>
                ";
        return $str;
        
    } 
    
    function oneBlock($url, $changefreq, $priority)
    {
        return "
        <url>
           <loc>".$url."</loc>
           <changefreq>".$changefreq."</changefreq>
           <priority>".$priority."</priority>
        </url>";
    }
    
    function backup()
    {
        //$backupFile = 'abc.sql';
        $command = "mysqldump -u root -p book store > dumpfilename.sql";
        system($command);
    }
}



?>